package heven.holt.library.paging

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.recyclerview.widget.RecyclerView
import heven.holt.library.databinding.LayoutLoadStateFooterMoreBinding

class LoadStateFooterMoreAdapter(
    private val retry: () -> Unit
) :
    LoadStateAdapter<LoadStateFooterMoreAdapter.LoadStateFooterMoreViewHolder>() {

    class LoadStateFooterMoreViewHolder(
        val binding: LayoutLoadStateFooterMoreBinding,
        retry: () -> Unit
    ) : RecyclerView.ViewHolder(binding.root) {
        init {
            binding.root.setOnClickListener { retry.invoke() }
        }
    }

    override fun onBindViewHolder(holder: LoadStateFooterMoreViewHolder, loadState: LoadState) {
        holder.binding.loadState = loadState
        holder.binding.executePendingBindings()
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        loadState: LoadState
    ): LoadStateFooterMoreViewHolder {
        return LoadStateFooterMoreViewHolder(
            LayoutLoadStateFooterMoreBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            ), retry
        )
    }
}